package smetana.modules.ui.form.doc;

import com.google.inject.Inject;
import org.apache.pivot.wtk.*;
import org.apache.pivot.wtkx.WTKX;
import smetana.domain.raw.Agent;
import smetana.domain.raw.Doc;
import smetana.domain.raw.Manager;
import smetana.modules.business.BusinessAgent;
import smetana.modules.business.BusinessDoc;
import smetana.modules.business.BusinessManager;
import smetana.modules.ui.form.AbstractPanel;
import smetana.modules.ui.form.doc.ProductsCartPanel;
import smetana.modules.ui.form.doc.ProductsCatalogPanel;
import smetana.modules.ui.form.search.SearchAgentDialog;
import smetana.modules.ui.form.search.SearchManagerDialog;
import smetana.modules.ui.model.CursorDoc;
import smetana.modules.ui.model.UIModel;
import smetana.domain.Id;
import smetana.util.Pivot;

/**
 *
 * @author dmitry.mamonov
 */
public class DocEditPanel extends AbstractPanel {
    @WTKX
    protected Form form;

    @WTKX
    protected CalendarButton fieldDateActual;

    @WTKX
    protected TextInput fieldUserCode;

    @WTKX
    protected TextInput fieldAgent;

    @WTKX
    protected PushButton buttonAgent;

    @WTKX
    protected TextInput fieldAddress;

    @WTKX
    protected TextInput fieldDescription;

    @WTKX
    protected TextInput fieldManager;

    @WTKX
    protected PushButton buttonManager;

    @Inject
    protected BusinessManager businessManager;

    @Inject
    protected BusinessAgent businessAgent;

    @Inject
    protected BusinessDoc businessDoc;

    @Inject
    protected UIModel uiModel;

    @Inject
    CursorDoc cursorDoc;

    @Inject
    protected SearchAgentDialog searchAgentDialog;

    @Inject
    protected SearchManagerDialog searchManagerDialog;



    protected Agent agent;

    protected Manager manager;

    protected Action actionAgent = new Action() {
        @Override
        public void perform() {
            searchAgentDialog.getDialog().open(getComponent().getWindow(), new DialogCloseListener() {
                @Override
                public void dialogClosed(Dialog dialog, boolean modal) {
                    if (dialog.getResult() == true) {
                        Agent agent = searchAgentDialog.getResult();
                        setAgent(agent);
                        store();
                        fieldAddress.requestFocus();
                    }
                    fieldAddress.clearSelection();
                }
            });
        }
    };

    protected Action actionManager = new Action() {
        @Override
        public void perform() {
            searchManagerDialog.getDialog().open(getComponent().getWindow(), new DialogCloseListener() {
                @Override
                public void dialogClosed(Dialog dialog, boolean modal) {
                    if (dialog.getResult() == true) {
                        Manager manager = searchManagerDialog.getResult();
                        setManager(manager);
                        store();
                    }
                }
            });
        }
    };

    @Inject
    ProductsCatalogPanel productsCatalogPanel;

    @WTKX
    SplitPane splitPane;

    @Inject
    ProductsCartPanel productsCartPanel;

    public DocEditPanel() {
        super();
    }

    @Override
    public void init() {
        buttonAgent.setAction(actionAgent);
        buttonManager.setAction(actionManager);

        splitPane.setTop(productsCatalogPanel.getComponent());
        splitPane.setBottom(productsCartPanel.getComponent());
    }

    public void load() {
        Doc document=cursorDoc.get();
        form.load(document);
        //setup agent
        this.setAgent(businessAgent.findAgentById(document.getAgentId()));
        //setup manager
        this.setManager(businessManager.findManagerById(document.getManagerId()));
    }

    protected void setAgent(Agent agent) {
        this.agent = agent;
        if (this.agent == null) {
            this.fieldAgent.setText("<Не указан>");
        } else {
            this.fieldAgent.setText(this.agent.getAgentName());
        }
    }

    protected void setManager(Manager manager) {
        this.manager = manager;
        if (this.manager == null) {
            this.fieldManager.setText("<Не указан>");
        } else {
            this.fieldManager.setText(this.manager.getManagerName());
        }
    }

    public void store() {
        Doc document = cursorDoc.get();
        form.store(document);
        document.setAgentId(Id.get(agent));
        document.setManagerId(Id.get(manager));
        businessDoc.saveDoc(document);
    }
}
